// Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.

#include "cef/libcef/browser/net_service/cookie_helper.h"
#include "cef/ohos_cef_ext/libcef/browser/net_service/arkweb_cookie_helper_ext.h"

#include "arkweb/build/features/features.h"
#include "content/public/browser/browser_context.h"
#include "services/network/public/cpp/resource_request.h"

#if BUILDFLAG(ARKWEB_EXT_EXCEPTION_LIST)
#include "base/ranges/algorithm.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#endif

namespace net_service::cookie_helper {

#if BUILDFLAG(ARKWEB_EXT_EXCEPTION_LIST)
bool CanSaveOrLoadCookies(
    const CefBrowserContext::Getter& browser_context_getter,
    const network::ResourceRequest& request) {
  auto cef_browser_context = GetBrowserContext(browser_context_getter);
  auto browser_context =
      cef_browser_context ? cef_browser_context->AsBrowserContext() : nullptr;
  if (!browser_context) {
    LOG(ERROR) << "Can not get browser_context.";
    return true;
  }

  HostContentSettingsMap* host_content_settings_map =
      HostContentSettingsMapFactory::GetForProfile(browser_context);
  if (!host_content_settings_map) {
    LOG(ERROR) << "Can not get host_content_settings_map.";
    return true;
  }

  ContentSettingsForOneType cookie_settings =
      host_content_settings_map->GetSettingsForOneType(
          ContentSettingsType::COOKIES);

  const auto& entry = base::ranges::find_if(
      cookie_settings, [&](const ContentSettingPatternSource& entry) {
        // The primary pattern is for the request URL; the secondary pattern
        // is for the first-party URL (which is the top-frame origin [if
        // available] or the site-for-cookies).
        return !entry.IsExpired() &&
               entry.primary_pattern.Matches(request.url) &&
               entry.secondary_pattern.Matches(request.url);
      });
  const ContentSettingPatternSource* match =
      (entry == cookie_settings.end() ? nullptr : &*entry);
  return !(match && match->GetContentSetting() == CONTENT_SETTING_BLOCK);
}
#endif

}  // namespace net_service::cookie_helper
